//
//  ZBUserDefultManger.m
//  ZuoBiao
//
//  Created by stoneobs on 17/2/23.
//  Copyright © 2017年 shixinyun. All rights reserved.
//

#import "YYUserLoginedDefultManger.h"
#import "STMusic-Swift.h"
@implementation YYUserLoginedDefultManger


+ (YYUserLoginedDefultManger *)sharedManger
{
    static dispatch_once_t onceToken;
    static YYUserLoginedDefultManger * sharedManger = nil;
    dispatch_once(&onceToken, ^{
        sharedManger = [YYUserLoginedDefultManger new];
    });
    return sharedManger;
}
- (YYUserModel *)currentUser
{
    return self.allUsers.firstObject;
}
- (NSString *)token
{
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:YYLOGIN_TOKEN];
    return token;
}


- (void)removeToken
{
 
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:YYLOGIN_TOKEN];
    [[NSUserDefaults standardUserDefaults] synchronize];

}
- (void)updateUserDefult:(YYUserModel *)userModel
{
    // TODO 在这里直接保存登录过得用户， 按这个格式存储
    /*
     @{
     LOGINED_USERS_KEY: @{
     @"objetcId1": userDic,
     @"objetcId2": userDic2
     }
     }
     */
    
    
    // 取userdefault中的数据
    id loginUserValue = [[NSUserDefaults standardUserDefaults] objectForKey:LOGINED_USERS_KEY];
    
    if (!loginUserValue
        || [loginUserValue isKindOfClass:[NSArray class]]) {
        NSArray *array = (NSArray *)loginUserValue;
        NSMutableArray *mutLoginUserArray = [NSMutableArray arrayWithArray:array];
        
        BOOL userExist = NO;
        for (int i =0; i< mutLoginUserArray.count; i++) {
            NSDictionary * tmpUserDic = mutLoginUserArray[i];
            if ([tmpUserDic.allKeys.firstObject  isEqualToString:userModel.objectId]) {
                userExist = YES;
                //存在用户，更改下标到第一个,可能会有信息更新，存入最新的model -> dic
                
                [mutLoginUserArray removeObjectAtIndex:i];
                NSString *objetcId = userModel.objectId;
                NSMutableDictionary *userModelDic = [userModel mj_keyValues];
                NSDictionary *userDic = [NSDictionary dictionaryWithObject:userModelDic forKey:objetcId];
                [mutLoginUserArray insertObject:userDic atIndex:0];
                [[NSUserDefaults standardUserDefaults] setObject:mutLoginUserArray forKey:LOGINED_USERS_KEY];
                
                break;
            }
        }
        
        // 不存在改用户
        if (!userExist) {
            // model --> dic
            NSString *userId = userModel.objectId;
            NSMutableDictionary *userModelDic = [userModel mj_keyValues];
            NSDictionary *userDic = [NSDictionary dictionaryWithObject:userModelDic forKey:userId];
            
            [mutLoginUserArray insertObject:userDic atIndex:0];
            [[NSUserDefaults standardUserDefaults] setObject:mutLoginUserArray forKey:LOGINED_USERS_KEY];
        }
    } else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:LOGINED_USERS_KEY];
    }
    // 同步
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSMutableArray<YYUserModel *> *)allUsers
{
    NSMutableArray * allUsers = nil;
    id userDefultValue = [[NSUserDefaults standardUserDefaults] objectForKey:LOGINED_USERS_KEY];
    if (userDefultValue
        && [userDefultValue isKindOfClass:[NSArray class]]) {
        NSArray *userDefaultArray = (NSArray *)userDefultValue;
        allUsers = [NSMutableArray arrayWithCapacity:userDefaultArray.count];
        //修改成model类型
        for (NSDictionary * dic in userDefaultArray) {
            NSDictionary * userDic = dic.allValues.firstObject;
            YYUserModel *userModel = [YYUserModel mj_objectWithKeyValues:userDic];
            [allUsers addObject:userModel];
        }
    }
    return allUsers;
}

- (void)removeUserFormObjectId:(NSString*)objectId;
{
    id userLoginValue = [[NSUserDefaults standardUserDefaults] objectForKey:LOGINED_USERS_KEY];
    if ([userLoginValue isKindOfClass:[NSArray class]]) {
        NSArray *userLoginArray = (NSArray *)userLoginValue;
        NSMutableArray *mutUserLoginArray = [NSMutableArray arrayWithArray:userLoginArray];
        NSInteger deleteIndex = NSNotFound;
        for (int i=0; i<mutUserLoginArray.count; ++i) {
            NSDictionary * userDic = mutUserLoginArray[i];
            if ([userDic.allKeys.firstObject  isEqualToString:objectId]) {
                deleteIndex = i;
            }
        }
        if (deleteIndex != NSNotFound) {
            [mutUserLoginArray removeObjectAtIndex:deleteIndex];
            [[NSUserDefaults standardUserDefaults] setObject:mutUserLoginArray forKey:LOGINED_USERS_KEY];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
}
- (YYUserModel *)userByAccount:(NSString *)account
{
    for (YYUserModel * model in self.allUsers) {
        if (model.username == account) {
            return model;
        }
    }
    return nil;
}
@end
